-- 对象池管理对象基类
local CanPoolObj = class("CanPoolObj")
local GameUtils = require("utils.GameUtils")

function CanPoolObj:ctor(resource)
    local prefabPath = resource.path .. "/" .. resource.name
    local parent = resource.parent
    local object = G_ResourceManager:loadPrefabs(prefabPath)
    local gameObject = nil
    if parent then
        gameObject = GameUtils.instantiate(object, parent.transform)
    else
        gameObject = GameUtils.instantiate(object)
    end
    local luaBehaviour = gameObject:GetComponent(typeof(CS.LuaBehaviour))
    if luaBehaviour then
        local scriptEnv = luaBehaviour:GetScriptEnv()
        assert(scriptEnv == nil, "load gameobject by Uibase scriptEnv must nil")
        luaBehaviour:SetScriptEnv(self)
    else
        assert(false, "can not load gameobject by Uibase the LuaBehaviour not found")
    end
end

function CanPoolObj:reset()
end

function CanPoolObj:setObjectPool(pool)
    self._objectPool = pool
end

function CanPoolObj:remove()
    self:onRemove()
    if self.isDestroy then
        return
    end
    if self._objectPool and not self._objectPool.isDestroy then
        self._objectPool:recyleObject(self)
    else
        self._objectPool = nil
        GameUtils.destroy(self.gameObject)
    end
end

function CanPoolObj:onRemove()
end

return CanPoolObj
